﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace Scheduler.Data_Access_Layer
{
    /**Class: EmpDayInfo
    * @author Daniel Chopson
    * @version 1.0
    * Course : ITEC 4860 Spring 2012
    * Written: March 22, 2012
    *
    * This class connects to the Employee and EmpDayInfo tables in the database via
    * TableAdapters. EmpDayInfo recods can be created. Getters and setters
    * are provided for modifying the fields in bulk.
    *
    * Purpose: To provide the data access neccessary for creating and modifying EmpDayInfo
    * records in the Employee Preferences module.
    */

    class EmpDayInfo
    {
        //The employee's ID

        private string empUserID;

        //The day's ID (i.e., a day of the week)

        private string dayID;

        //The adapter for EmpDayInfo table connectivity

        private SchedulerTableAdapters.EmpDayInfoTableAdapter empDayAdapter;

        //The data table for temporarily holding EmpDayInfo data
        
        private Scheduler.EmpDayInfoDataTable empDayData;

        //A single row from the EmpDayInfo table

        private Scheduler.EmpDayInfoRow empDayRow;

        /** Method: EmpDayInfo
          * Constructor for this class. Instantiates all class variables
          * @param empUserID the employee's ID
          * @param dayID the day's ID
          */

        public EmpDayInfo(string empUserID, string dayID)
        {
            this.empUserID = empUserID;
            this.dayID = dayID;
            empDayAdapter = new SchedulerTableAdapters.EmpDayInfoTableAdapter();
        }

        /** Method: createEmpDay
          * Creates a record in the EmpDayInfo table
          * @param empUserID the employee's ID
          * @param dayID the day's ID
          * @return an ArrayList of SQL error messages for error handling in the Presentation_Layer
          */

        public ArrayList createEmpDay(string empUserID, string dayID)
        {
            ArrayList errorMessages = new ArrayList();
            try
            {
                //empDayAdapter.InsertWithoutAvailHrs(empUserID, dayID);
                //update per intellisense sft 05/22 5:22PM
                empDayAdapter.InsertWithoutAvailHrs(dayID, empUserID);
            }
            catch (SqlException exception)
            {
                for (int i = 0; i < exception.Errors.Count; i++)
                {
                    ErrorLookup errorLookup = new ErrorLookup(exception.Errors[i].Number);
                    errorMessages.Add(errorLookup.returnSimpleError());
                }
            }
            return errorMessages;
        }

        //public int createEmptyEmpDay(string empUserID, string dayID)
        //{
        //    int rc = 0;
        //    try
        //    {
        //        empDayAdapter.InsertWithEmptyHours(
        //        }
        //    return rc;
        //}

        /** Method: locateEmpDay
          * Locates the row in the EmpDayInfo table with the specified employee ID and day ID
          * Note: Must be called before the methods following it can be used
          * @return a boolean to denote whether the user was found
          */

        public bool locateEmpDay()
        {
            empDayData = empDayAdapter.GetDataByDayIDEmpID(dayID, empUserID);
            if (empDayData.Count == 0)
            {
                return false;
            }
            else
            {
                empDayRow = empDayData[0];
            }
            return true;
        }

        /** Method: getAllAvailHr
          * Gets all of the fields for a given row
          * @return an Array of database fields
          */

        public Array getAllAvailHr()
        {
            return empDayRow.ItemArray;
        }

        /** Method: setAllAvailHr
          * Sets all of the fields for a given row
          * @param availHrArray an Array of all fields to be saved to the database
          */
        
        public void setAllAvailHr(Array availHrArray)
        {
            empDayRow.ItemArray = (object[])availHrArray;
            empDayAdapter.Update(empDayData);
        }
    }
}
